package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.bo.NewArticleBO;
import com.imooc.enums.ArticleStatus;
import com.imooc.pojo.Article;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.ArticleService;
import com.imooc.utils.PagedGridResult;
import com.imooc.vo.ArticleVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liansen
 * @create 06-16-18:15
 */
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseInfoProperties {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //发布文章
    @PostMapping("/save")
    public GraceJSONResult createArticle(@RequestBody @Validated NewArticleBO newArticleBO){
        String articleId = newArticleBO.getArticleId();
        if(StringUtils.isBlank(articleId)){
            articleService.createArticle(newArticleBO);
        }else {
            articleService.updateArticle(newArticleBO);
        }
        return GraceJSONResult.ok();
    }

    //修改文章状态
    @PostMapping("/update")
    public GraceJSONResult updateStatus(@RequestParam("articleId") String articleId,
                                        @RequestParam("status") Integer status){
        articleService.updateStatus(articleId, status);
        return GraceJSONResult.ok();
    }

    //查询文章列表(pageHelper分页)
    @PostMapping("/list")
    public GraceJSONResult list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        PagedGridResult result = articleService.queryList(null, page, limit);
        result = dealReadCounts(result);
        return GraceJSONResult.ok(result);
    }

    //通过文章id查询文章详情
    @PostMapping("/detail")
    public GraceJSONResult detail(@RequestParam("articleId") String articleId){
        if(StringUtils.isBlank(articleId)){
            return GraceJSONResult.error();
        }
        Article article = articleService.detail(articleId);
        return GraceJSONResult.ok(article);
    }

    @PostMapping("/delete")
    public GraceJSONResult delete(@RequestParam("articleId") String articleId){
        articleService.updateStatus(articleId, ArticleStatus.DELETE.type);
        return GraceJSONResult.ok();
    }

    @PostMapping("/open")
    public GraceJSONResult open(@RequestParam("articleId") String articleId){
        articleService.updateStatus(articleId, ArticleStatus.OPEN.type);
        return GraceJSONResult.ok();
    }

    @PostMapping("/close")
    public GraceJSONResult close(@RequestParam("articleId") String articleId){
        articleService.updateStatus(articleId, ArticleStatus.CLOSE.type);
        return GraceJSONResult.ok();
    }

    //***************************以下提供给app端****************************************

    @PostMapping("/app/list")
    public GraceJSONResult lists(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        PagedGridResult result = articleService.queryList(null, page, limit);
        result = dealReadCounts(result);
        return GraceJSONResult.ok(result);
    }

    @PostMapping("/app/detail")
    public GraceJSONResult details(@RequestParam("articleId") String articleId){
        if(StringUtils.isBlank(articleId)){
            return GraceJSONResult.error();
        }
        Article article = articleService.detail(articleId);
        return GraceJSONResult.ok(article);
    }

    //用户阅读数的累加与防刷
    @PostMapping("/app/read")
    public GraceJSONResult read(String userId, String articleId){

        String isRead = redisTemplate.opsForValue().get(REDIS_USER_READ_ARTICLE + ":" + userId + ":" + articleId);

        if(StringUtils.isBlank(isRead)){
            //阅读数量加 1
            redisTemplate.opsForValue().increment(REDIS_ARTICLE_READ_COUNTS + ":" + articleId, 1);

            //标记用户阅读
            redisTemplate.opsForValue().set(REDIS_USER_READ_ARTICLE + ":" + userId + ":" + articleId, articleId);
        }

        return GraceJSONResult.ok();
    }

    private PagedGridResult dealReadCounts(PagedGridResult result){

        List<Article> rows = (List<Article>) result.getRows();
        List<ArticleVO> collect = rows.stream().map(article -> {
            ArticleVO articleVO = new ArticleVO();
            String articleId = article.getId();
            String readCounts = redisTemplate.opsForValue().get(REDIS_ARTICLE_READ_COUNTS + ":" + articleId);
            if(StringUtils.isBlank(readCounts)){
                readCounts = "0";
            }
            BeanUtils.copyProperties(article, articleVO);
            articleVO.setReadCounts(Long.parseLong(readCounts));
            return articleVO;
        }).collect(Collectors.toList());
        result.setRows(collect);
        return result;
    }

}
